var url = '../../../public/index.php/';     //todo 请求路径
var limit = 10;
//todo 消息弹窗
function lay(msg,time=0){
    if(time <= 0){
        time = 1000;
    }
    layer.msg(msg,{
        time:time,
    })
}
//todo 询问弹窗
function layFirm(title,urls,data,method='get'){
    layer.confirm('您确定要'+title+'这个',{
        btn:[title,'取消'],
    },function(){
        $.ajax({
            url:url+urls,
            type:method,
            contentType:'JSON',
            data:data,
            dataType:'json',
            success:function(data){
                if(data.code === 1){
                    lay('操作成功');
                    setTimeout(function(){
                        window.parent.close();
                        location.reload();
                    },1500);
                }else if(data.code === 0){
                    lay(data.msg)
                }else{
                    lay(data.msg);
                    setTimeout(function(){
                        window.parent.close();
                        window.parent.location.href = '../../index.html'
                    },1500);
                }
            },
            error:function(){
                lay('网络错误');
            },
        })
    },function(){
        layer.close();
    })
}

//todo 页面弹窗
function layOpen(title,width,height,urls){
    layer.open({
        type:2,
        title:title,
        shadeClose:true,
        shade:0.8,
        area:[width,height],
        content:urls,
    })
}

//todo 截取地址栏参数

function GetQueryString(name)
{
    let reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    let r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}

//todo 关闭弹窗ajax提交表单
$('#ajax-btn').click(function(){
    let data = $('#ajaxForm').serialize();
    let urls = $('#ajaxForm').attr('url');
    let type = $('#ajaxForm').attr('method');
    let close = $('#ajaxForm').attr('close');
    $.ajax({
        url:url+urls,
        type:type,
        data:data,
        dataType:'json',
        success:function(data){
            if(data.code === 1){
                lay('操作成功');
                setTimeout(function () {
                    if(close == 'no'){
                        location.reload();
                    }else{
                        window.parent.location.reload();
                        parent.layer.close();
                    }
                },1500)
            }else if(data.code == -1){
                lay(data.msg);
                setTimeout(function(){
                    window.parent.close();
                    window.parent.location.href = '../../index.html'
                },1500);
            }else{
                lay(data.msg)
            }
        },
        error:function(){
            lay('网络错误');
        }
    })
});

//todo count = 数据总条数 page = 当前页码 herf =跳转的页面
//todo 渲染元素id = pagebox
function WepPage(count,page,herf,limit=10){
    page = Number(page);
    if(page <=0)page = 1;
    let pagecount = Math.ceil(count/limit);
    if(pagecount > 1){
        let uppage = `<a class="pagedir" href="${herf}.html?page=${Math.abs(page-1)}">↥</a>`;
        if(page==1) uppage =``;
        let downpage = `<a class="pagedir" href="${herf}.html?page=${Math.abs(page+1)}">↧</a>`;
        if(page == pagecount)downpage =``;
        let html = `
        <input value="${page}" class="pagenow">
        <span class="pagecount">
            &nbsp;&nbsp;/&nbsp;&nbsp;${pagecount}
        </span>
        ${uppage}
        ${downpage}
        <a class="pagedir" id="tiaozhuan">跳转</a>
    `;
        $('#pagebox').html(html);
        $('#tiaozhuan').click(function(){
            let tiao = $('.pagenow').val();
            window.location.href = herf+'.html?page='+tiao;
        });
    }
}

//todo 数据曲线图化


//todo 图片上传并显示
//todo inputId 上传图片的input 标签id
//todo putImg  保存图片地址的input 标签id
//todo showImg 展示图片地址的img 标签id
function imgPut(inputId,putImg,showImg,urls){
    let formData = new FormData;
    let img = document.getElementById(inputId);
    let fileObj = img.files[0];
    formData.append('data',fileObj);
    $('#'+showImg).attr('src','http://pekahemgt.bkt.clouddn.com/loading.gif')
    $.ajax({
        url:url+urls,
        type:"POST",
        data:formData,
        async:false,
        processData:false,
        contentType:false,
        dataType:'json',
        success:function(data){
            if(data.code == 1){
                $('#'+putImg).val(data.data);
                $('#'+showImg).attr('src',data.data)
            }else if(data.code == 0){
                lay(data.msg);
            }else{
                lay(data.msg);
                setTimeout(function(){
                    window.parent.close();
                    window.parent.location.href = '../../index.html'
                },1500);
            }

        },
        error:function(){
            lay('Api Error');
        }
    })
}

//todo 转换时间戳
function formatDateTime(inputTime) {
    let date = new Date(inputTime*1000);
    let y = date.getFullYear();
    let m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    let d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    let h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    let minute = date.getMinutes();
    let second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
}

//todo  封装的ajax请求
//todo urls 请求地址  method 请求方式 datas请求参数
function AjaxForm(urls,method,datas=false,close=1) {
    let data;
    $.ajaxSetup({
        async: false
    });
    $.ajax({
        url:url+urls,
        type:method,
        data:datas,
        dataType:'json',
        success:function(res){
            if(res.code === 1){
                data = res.data;
            }else if(res.code == -1){
                lay(res.msg);
                setTimeout(function(){
                    window.parent.close();
                    window.parent.location.href = '../../index.html'
                },1000);
            }else{
                lay(res.msg);
                if(close == 1){
                    setTimeout(function(){
                        window.parent.location.reload();
                    },1500);
                }
            }
        },
        error:function(){
            lay('网络错误');
            return
        }
    });
    return data;
}


